package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.HabitanteCalle;

public class HabitanteCalleDAOImpl extends JdbcDaoSupport implements IHabitanteCalleDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM OPE_HABITANTE_CALLE_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(HabitanteCalle habcraslinf) {
		deleteOperation.execute( habcraslinf  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public HabitanteCalle findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(HabitanteCalle habcraslinf) {
		insertOperation.execute( habcraslinf );
	}

	public void update(HabitanteCalle habcraslinf) {
		updateOperation.execute( habcraslinf );
	}

	public HabitanteCalle findByCriteria(HabitanteCalle  habcraslinf) {
		return findByCriteriaQuery.runQuery(habcraslinf);
	}

	public List load(HabitanteCalle  habcraslinf) {
		return findAllQuery.runQuery(habcraslinf);
	}

	public HabitanteCalle findByActiveState(HabitanteCalle  habcraslinf) {
		return findByActiveStateQuery.runQuery(habcraslinf);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	deleteOperation = new DeleteOperation(  getDataSource() );
    	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO OPE_HABITANTE_CALLE_T ("
				  + " ID, "
				  
				  + " CODIGO, "
				  
				  + " DIRECCION_PUNTO_HABC, "
				  + " CODIGO_BARRIO, "
				  + " BARRIO, "
				  + " CODIGO_COMUNA, "
				  + " COMUNA, "
				  
				  + " ID_ACTIVIDAD_TIPO, "
				  
				  + " FECHA_REGISTRO, "
				  + " HORA, "
				  + " NUMERO_HABC, "
				  + " OBSERVACIONES, "
				  + " ESTADO, "
				  
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  
				  + ") VALUES ("
				  
			  	  + " OPE_HABC_TRASL_INF_SEC.nextVal, "
			  	  
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(HabitanteCalle habcraslinf) {
			InsertOperation.this.update( new Object[]{
				habcraslinf.getCodigo(),
				
				habcraslinf.getDireccionPuntoHabc(),
				habcraslinf.getCodigoBarrio(),
				habcraslinf.getBarrio(),
				habcraslinf.getCodigoComuna(),
				habcraslinf.getComuna(),
				
				habcraslinf.getIdActividadTipo(),
				
				habcraslinf.getFechaRegistro(),
				habcraslinf.getHora(),
				habcraslinf.getNumeroHabc(),
				habcraslinf.getObservaciones(),
				habcraslinf.getEstado(),
				
				habcraslinf.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE OPE_HABITANTE_CALLE_T SET "
					  + " CODIGO = ? ,"
					  
					  + " DIRECCION_PUNTO_HABC = ? ,"
					  + " CODIGO_BARRIO = ? ,"
					  + " BARRIO = ? ,"
					  + " CODIGO_COMUNA = ? ,"
					  + " COMUNA = ? ,"
					  
					  + " ID_ACTIVIDAD_TIPO = ? ,"
					  
					  + " FECHA_REGISTRO = ? ,"
					  + " HORA = ? ,"
					  + " NUMERO_HABC = ? ,"
					  + " OBSERVACIONES = ? ,"
					  + " ESTADO = ? ,"
					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
					  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));

			compile();
		}
		public void execute(HabitanteCalle habcraslinf) {
			UpdateOperation.this.update( new Object[]{
				
				habcraslinf.getCodigo(),
				
				habcraslinf.getDireccionPuntoHabc(),
				habcraslinf.getCodigoBarrio(),
				habcraslinf.getBarrio(),
				habcraslinf.getCodigoComuna(),
				habcraslinf.getComuna(),
				
				habcraslinf.getIdActividadTipo(),
				
				habcraslinf.getFechaRegistro(),
				habcraslinf.getHora(),
				habcraslinf.getNumeroHabc(),
				habcraslinf.getObservaciones(),
				habcraslinf.getEstado(),

				habcraslinf.getUsuarioModifica(),
				
				habcraslinf.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_HABITANTE_CALLE_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(HabitanteCalle habcraslinf) {
			this.update( new Object[]{
				habcraslinf.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_HABITANTE_CALLE_T "
										+ WHERE
										+ " 1=1 ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			HabitanteCalle habcraslinf = new HabitanteCalle();
				habcraslinf.setId(new Long(rs.getLong("ID")));
				habcraslinf.setCodigo(rs.getString("CODIGO"));
				habcraslinf.setDireccionPuntoHabc(rs.getString("DIRECCION_PUNTO_HABC"));
				habcraslinf.setCodigoBarrio(rs.getString("CODIGO_BARRIO"));
				habcraslinf.setBarrio(rs.getString("BARRIO"));
				habcraslinf.setCodigoComuna(rs.getString("CODIGO_COMUNA"));
				habcraslinf.setComuna(rs.getString("COMUNA"));
				habcraslinf.setIdActividadTipo(new Long(rs.getLong("ID_ACTIVIDAD_TIPO")));
				habcraslinf.setFechaRegistro(rs.getDate("FECHA_REGISTRO"));	
				habcraslinf.setHora(rs.getString("HORA"));
				habcraslinf.setNumeroHabc(rs.getString("NUMERO_HABC"));
				habcraslinf.setObservaciones(rs.getString("OBSERVACIONES"));
				habcraslinf.setEstado(rs.getString("ESTADO"));
				habcraslinf.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				habcraslinf.setFechaCrea(rs.getDate("FECHA_CREA"));		
				habcraslinf.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				habcraslinf.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return habcraslinf;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
										+ " 1=1 ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public HabitanteCalle runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (HabitanteCalle) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND DIRECCION_PUNTO_HABC = ? "
			  	  	  					//+ " AND CODIGO_BARRIO = ? "
			  	  	  					//+ " AND BARRIO = ? "
			  	  	  					//+ " AND CODIGO_COMUNA = ? "
			  	  	  					//+ " AND COMUNA = ? "
			  	  	  					//+ " AND ID_ACTIVIDAD_TIPO = ? "
			  	  	  					//+ " AND FECHA_REGISTRO = ? "
			  	  	  					//+ " AND HORA = ? "
			  	  	  					//+ " AND NUMERO_HABC = ? "
			  	  	  					//+ " AND OBSERVACIONES = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public HabitanteCalle runQuery(HabitanteCalle habcraslinf) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//habcraslinf.getId(),
				//habcraslinf.getCodigo(),
				//habcraslinf.getDireccionPuntoHabc(),
				//habcraslinf.getCodigoBarrio(),
				//habcraslinf.getBarrio(),
				//habcraslinf.getCodigoComuna(),
				//habcraslinf.getComuna(),
				//habcraslinf.getIdActividadTipo(),
				//habcraslinf.getHora(),
				//habcraslinf.getNumeroHabc(),
				//habcraslinf.getObservaciones(),
				//habcraslinf.getEstado(),
				//habcraslinf.getUsuarioCrea(),
				//habcraslinf.getFechaCrea(),
				//habcraslinf.getUsuarioModifica(),
				//habcraslinf.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (HabitanteCalle) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
										+ " CODIGO LIKE ? ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(HabitanteCalle habcraslinf) {
    		List result =  execute( new Object[]{
					habcraslinf.getCodigo()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
										+ " CODIGO LIKE ? ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public HabitanteCalle runQuery(HabitanteCalle habcraslinf) {
			List result =  execute( new Object[]{
					habcraslinf.getCodigo()
			});
			if ( result.size() > 0 ) {
        		return (HabitanteCalle) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
